package com.hmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.hmall.common.constants.MqConstants.*;

@Configuration
public class RabbitMQConfig {
    //TTL + 死信队列 实现延时消息
    //1、声明一组普通交换机和队列
    @Bean
    public DirectExchange ttlDirectExchange(){
        return new DirectExchange(HMALL_TTL_DIRECT);
    }

    @Bean
    public Queue ttlQueue(){
        return QueueBuilder.durable(HMALL_TTL_QUEUE)  // 指定队列名称，并持久化
                .ttl(20000)     //设置队列的超时时间，20秒
                .deadLetterExchange(HMALL_DL_DIRECT)     // 指定死信交换机
                .deadLetterRoutingKey(HMALL_DL_KEY)      //指定死信routingKey
                .build();
    }

    @Bean
    public Binding ttlBinding(){
        return BindingBuilder.bind(ttlQueue()).to(ttlDirectExchange()).with(HMALL_TTL_KEY);
    }

    //TTL + 死信队列 实现延时消息
    //2、声明一组死信交换机和队列
    @Bean
    public DirectExchange dlMessageExchange(){
        return new DirectExchange(HMALL_DL_DIRECT);
    }

    @Bean
    public Queue dlQueue(){
        return new Queue(HMALL_DL_QUEUE);
    }

    @Bean
    public Binding dlMessageBinding(){
        return BindingBuilder.bind(dlQueue()).to(dlMessageExchange()).with(HMALL_DL_KEY);
    }


    /**
     * DelayExchange 延时插件实现 延时消息
     * 声明一组延时交换机和队列
     */
    /*@Bean
    public DirectExchange delayedExchange(){
        return ExchangeBuilder.directExchange(HMALL_DELAY_DIRECT).delayed().build();
    }

    @Bean
    public Queue delayQueue(){
        return new Queue(HMALL_DELAY_QUEUE);
    }

    @Bean
    public Binding delayedBinding(){
        return BindingBuilder.bind(delayQueue()).to(delayedExchange()).with(HMALL_DELAY_KEY);
    }*/

}
